import pandas as pd


def read_dataset_info(dataset_info_file: str) -> pd.DataFrame:
    with open(dataset_info_file, 'r', encoding='utf-8') as file:
        dataset_info = file.read()
    ds_arr = dataset_info.split("####")[1:]

    dm_table_field = pd.DataFrame(columns=['uid', '字段PG'])

    for i, ds_one in enumerate(ds_arr):
        entity = ds_one.split("--")[0].strip()
        old_table = ds_one.split("--")[1].split(",")[0].strip().upper()
        new_table = ds_one.split("--")[1].split(",")[1].strip().upper()
        field_arr = ds_one.split("--")[2].split('\n')
        for e in field_arr:
            if not e or not e.strip():
                continue
            field_pg = e.split(",")[0].strip().upper()
            field_dm = e.split(",")[1].strip().upper()
            dm_table_field.loc[len(dm_table_field)] = [
                f"{new_table}.{field_dm}",
                field_pg
            ]

    return dm_table_field


if __name__ == '__main__':
    info = read_dataset_info("dataset_info.txt")
    info.to_excel("gen/dataset_info.xlsx")
